package com.pwp.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.agent.crm.base.BaseActivity;
import com.agent.crm.view.R;
import com.pwp.borderText.BorderTextView;
import com.pwp.constant.CalendarConstant;
import com.pwp.dao.ScheduleDAO;
import com.pwp.vo.ScheduleVO;

/**
 * 显示/修改所有日程的activity
 * @author jack_peng
 *
 */
public class ScheduleAll extends BaseActivity {

	private ScrollView sv = null;
	private LinearLayout layout = null;
	private BorderTextView textTop = null;
	private ScheduleDAO dao = null;
	private ScheduleVO scheduleVO = null;
	private ArrayList<ScheduleVO> schList = new ArrayList<ScheduleVO>();
	private String scheduleInfo = "";
	private final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	
	private int scheduleID = -1;
	
	
	/**
	 * 得到所有的日程信息
	 */
	public void getScheduleAll(){
		schList = dao.getAllSchedule();
		if(schList != null){
			for (ScheduleVO vo : schList) {
				String content = vo.getScheduleContent();
				int startLine = content.indexOf("\n");
				if(startLine > 0){
					content = content.substring(0, startLine)+"...";
				}else if(content.length() > 30){
					content = content.substring(0, 30)+"...";
				}
				scheduleInfo = CalendarConstant.sch_type[vo.getScheduleTypeID()]+"\n"+vo.getScheduleDate()+"\n"+content;
				scheduleID = vo.getScheduleID();
				createInfotext(scheduleInfo, scheduleID);
			}
		}else{
			scheduleInfo = "没有日程";
			createInfotext(scheduleInfo,-1);
		}
	}
	
	/**
	 * 创建放日程信息的textview
	 */
	public void createInfotext(String scheduleInfo, int scheduleID){
		final BorderTextView info = new BorderTextView(this, null);
		info.setText(scheduleInfo);
		info.setTextColor(Color.BLACK); 
		info.setBackgroundColor(Color.WHITE);
		info.setLayoutParams(params);
		info.setGravity(Gravity.CENTER_VERTICAL);
		info.setPadding(10, 5, 10, 5);
		info.setTag(scheduleID);
		layout.addView(info);
		
//		//点击每一个textview就跳转到shceduleInfoView中显示详细信息
//		info.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				String schID = String.valueOf(v.getTag());
//				String scheduleIDs[] = new String[]{schID};
//				Intent intent = new Intent();
//				intent.setClass(ScheduleAll.this, ScheduleInfoView.class);
//				intent.putExtra("scheduleID", scheduleIDs);
//				startActivity(intent);
//			}
//		});
//		
		
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(1, menu.FIRST, menu.FIRST, "返回日历");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch(item.getItemId()){
		case Menu.FIRST:
			Intent intent = new Intent();
			intent.setClass(ScheduleAll.this, CalendarActivity.class);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub
		getScheduleAll();
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		dao = new ScheduleDAO(this);
		sv = new ScrollView(this);
		LinearLayout.LayoutParams paramParent=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		sv.setLayoutParams(paramParent);
		params.setMargins(0, 5, 0, 0);
		layout = new LinearLayout(this); // 实例化布局对象
		layout.setOrientation(LinearLayout.VERTICAL);
//		layout.setBackgroundResource(R.drawable.schedule_bk);
		layout.setBackgroundColor(Color.WHITE);
//		layout.setLayoutParams(params);
		paramParent.setMargins(0, 5, 0, 0);
		layout.setLayoutParams(paramParent);
		
//		textTop = new BorderTextView(this, null);
//		textTop.setTextColor(Color.BLACK); 
//		textTop.setBackgroundResource(R.drawable.top_day);
//		textTop.setText("所有日程");
//		textTop.setHeight(47);
//		textTop.setGravity(Gravity.CENTER);
		LinearLayout layoutHead=new LinearLayout(this);
		LayoutInflater inflaterHead =LayoutInflater.from(this);
		inflaterHead.inflate(R.layout.header_banner, layoutHead);
		layoutHead.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
		Button leftAddBtn=(Button)layoutHead.findViewById(R.id.bt_add_contacts);
		leftAddBtn.setVisibility(View.GONE);
		Button rightBtn=(Button)layoutHead.findViewById(R.id.title_bt_setting);
		rightBtn.setVisibility(View.GONE);
		TextView titleText=(TextView)layoutHead.findViewById(R.id.title_txt);
		titleText.setText("所有日程");
		titleText.setTextSize(18.0f);
		
		layout.addView(layoutHead);
		sv.addView(layout);
		return sv;
	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub
		customLayoutFlag=true;
	}
}
